var Guest=require('../models/guestModel.js');
var code=require('../txtCode/code.js');
var moment=require('moment');
// var async=require('async');
module.exports={
	GetListHtml:function(req,res,next){	
		res.render('guestList',function(err,html){
			if(err) console.log(err);
			res.send(html);
		});
	},
	GetAddHtml:function(req,res,next){
		res.render('guestAdd',function(err,html){
			if(err) console.log(err);
			res.send(html);
		});
	},
	WechatLogin:function(req,res,next){
			var data=req.body;
			Guest
			.find({wechatId:data.wechatId})
			.exec(function(err,docs){
				if(err){
					console.log(err);
					res.send({
						code:[1003,'出错'],
						data:err
					});
					return;
				};//if(err)

				if(docs.length>0){
						var today=moment();
						var diffEndToToday=moment(docs[0].validend,moment.ISO_8601).diff(today,'days');
							console.log(diffEndToToday);
						if(diffEndToToday<0){
							res.send({
								code:[1004,'已过有效期'],
								data:'已过有效期'										
							});
							return;
						};//if(today)

						if(docs[0].credit>3){
							res.send({
								code:[1002,'账号已被锁定']
							});
							return;
						}

						if(docs[0].guestcategory && (docs[0].guestcategory=='次卡客户' || docs[0].guestcategory=='私教客户')){
							if(docs[0].consumptime>=docs[0].totaltime){
								res.send({
									code:[1005,'课时不足,请补充课时'],
									data:'课时不足,请补充课时'
								});
								return;
							};//if(docs[0].consumptime>=docs[0].totaltime)
						};//if()
						
						res.send({
							code:[1000,'登陆成功'],
							mobile:docs[0].mobile
						});
						return;
				}

				if(docs.length==0){
					res.send({
							code:[1001,'尚未绑定手机号码']	
						});
				}
			});
	},//isGuest
	AddGuest:function(req,res,next){
		// if(req.session.token){
			var data=req.body;
			console.log(data);
		
			var oNewGuest=new Guest(data);
		 	oNewGuest.save().then(
		 		function(data){
		 				res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
			});//save()
		// }//if
	},//AddGuest,
	GetPageQuantity:function(req,res,next){
		if(req.session){
			Guest
		.find()
		.sort()
		.count(function(err,count){
			if(err) console.log(err);
			console.log(count);
			var pageQuantity=Math.ceil(count/10);
			res.send({
				code:code.success,
				data:{'pageQuantity':pageQuantity}});
			});
		};//if(req.session)
		},
	FirstShow:function(req,res,next){
		if(req.session){
			Guest
			.find()
			.sort()
			.limit(10)
			.then(
				function(result){
					res.send({code:code.success,data:result});
				}
				,function(err){
					if(err) console.log(err);
					res.send({code:code.findFail});
				}
				);//
			
		}//if(req.session)
	},//FirstShow
	ShowPage:function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			Guest
			//以下为原有代码
			// .find()
			// .sort()
			// .skip(pageNumber)
			// .limit(10)
			// .then(function(result){
			// 	console.log(result);
			// 	res.send({
			// 		code:[1000,'success'],
			// 		data:result
			// 	});
			// });
			//以上为原有代码
			.aggregate
			(	
				{
					$group:{
						_id:'$mobile',
						guestname:{$first:'$guestname'},
						mobile:{$first:'$mobile'},
						born:{$first:'$born'},
						logupDate:{$first:'$logupDate'}
					}
				},
				{
					$sort:{
						logupDate:1
					}
				},
				{
					$skip:pageNumber
				},
				{
					$limit:10
				},
				function(err,docs){
					if(err){
						console.log(err);
						res.send({
							code:[1001,'service is wrong'],
							data:err
						});
						return;
					}//if(err)
					res.send({
						code:[1000,'success'],
						data:docs
					});
				}
			)
		}//if(req.session)
	},//ShowPage
	FetchGuest:function(req,res,next){
		if(req.session){

			var needMobile=req.body.needMobile;

			var regex=new RegExp(needMobile);

			// Guest
			// .find()
			// .or([{guestname:regex},{mobile:regex}])

			Guest
			.aggregate(
				{
					$match:
					{
						$or:
							[{guestname:regex},{mobile:regex}]
					}
				},
				{
					$group:{
						_id:'$mobile',
						guestname:{$first:"$guestname"},
						mobile:{$first:'$mobile'},
						born:{$first:'$born'},
						logupDate:{$first:'$logupDate'}
					}
				},function(err,docs){
					if(err){
						console.log(err);
						res.send({
							code:[1001,'service is wrong'],
							data:err	
						});
						return;
					};//if(err)
					
					res.send({
						code:[1000,'success'],
						data:docs
					});//res.send

				}//function(err,docs)
				);
		}//if(req.session)
	},//FetchGuest
	FetchGuestBase:function(req,res,next){
		if(req.session){
				var needMobile=req.body.needMobile;
				Guest
				.find({mobile:needMobile})
				.exec(function(err,docs){
					if(err){
						console.log(err);
						res.send({
							code:[1001,'service is wrong'],
							data:err	
						});
						return;
					};//if(err)

					res.send({
						code:[1000,'success'],
						data:docs
					});
				})
		};//if(req.session)
	},//FetchGuestBase
	GetBaseInfoHtml:function(req,res,next){
		if(req.session){
			console.log(req.query.needGuest);
			console.log(typeof req.query.needGuest);
			var accountBaseInfo=JSON.parse(req.query.needGuest);
			console.log(typeof accountBaseInfo);
			console.log(accountBaseInfo);
			res.render('guestBaseInfo',accountBaseInfo,function(err,html){
				if(err) console.log(err);
				res.send(html);
			});
		}//if(req.session)
	},//GetBaseInfoHtml
	GetTiNengHtml:function(req,res,next){
		if(req.session){
			var accountTiNeng=JSON.parse(req.query.needGuest);
			res.render('guestTiNeng',accountTiNeng,function(err,html){
				if(err) console.log(err);
				res.send(html);
			});
		}//if(req.session)
	},//GetTiNengHtml
	MohuName:function(req,res,next){
		if(req.session){
			var regexString=req.body.mohuName;
			console.log(regexString);
			var regex=new RegExp(regexString);
			
			console.log(regex);
			Guest
			.find({'guestname':regex})
			.exec(function(err,items){
				if(err) console.log(err);
				console.log(items);
				res.send({
					data:items
				});
			});//Guest
		}//if(req.session)
	},//MohuName
	SearchCondition:function(req,res,next){
		if(req.session){
			// var data=req.body;
			// console.log(data);

			var regexString=req.body.guestname;
			console.log(regexString);
			var regex=new RegExp(regexString);
			
			console.log(regex);

			Guest
			.find({guestname:regex})
			.exec(function(err,result){
				if(err) {
					console.log(err);
					res.send({
						data:err
					});
					return;
				}
				res.send({
					data:result
				})
			});//exec
		}//if(req.session)
	},//GetTiNengHtml
	UpdateGuestBase:function(req,res,next){
		if(req.session){
			var mobile=req.body.mobile;
			console.log(mobile);

			var guest=req.body.guest;
			Guest
			.update({
				mobile:mobile
			},{
				$set:{
					guestname:guest.guestname,
					mobile:guest.mobile,
					guestcategory:guest.guestcategory,
					validstart:guest.validstart,
					validend:guest.validend,
					totaltime:guest.totaltime,
					sex:guest.sex,
					born:guest.born,
					target:guest.target,
					otherTarget:guest.otherTarget,
					region:guest.region,
					health:guest.health,
					health:guest.health,

				}
			},function(err,docs){
				if(err){
					console.log(err);
					res.send({
						data:err
					});
					return;
				};//if(err)

				res.send({
					data:docs
				});
			});//Guest
		};//if(req.session)
	},//UpdateGuestBase
	FetchGuestTiNeng:function(req,res,next){
		if(req.session){
			var mobile=req.body.mobile;
			Guest
			.find({
				mobile:mobile
			})
			.sort('lurutime')
			.exec(function(err,docs){
				if(err){
					console.log(err);
					res.send({
						data:err
					});
					return;
				};//if(err)

				console.log(docs);
				res.send({
					data:docs
				});
			});			
		};//if(req.session)
	},//FetchGuestTiNeng
	UpdateTiNeng:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			console.log(typeof data.lurutime);
			console.log(data.lurutime);
			data.lurutime=moment(data.lurutime,moment.ISO_8601);
			Guest
			.find({lurutime:data.lurutime})
			.exec(function(err,docs){
				if(err){
					console.log(err);
					res.send({
						data:err
					});
					return;
				};//if(err)
				if(docs.length==0){
					console.log(data['_id']);
					console.log(data._id);

					data['_id']=null;
					console.log(typeof data.lurutime);
					console.log(data.lurutime);
					// data.lurutime=moment(data.lurutime,moment.ISO_8601).add(8,'hours');
					var oNewGuest=new Guest(data);
					oNewGuest.save().then(function(data){
							res.send({
									code:[1000,'新增成功'],
									data:'新增成功'
								});
							return;
					},function(err){
						if(err){
							console.log(err);
								res.send({
									code:[1001,'出错'],
									data:err
								});
							return;
						};//if(err)
					});
				};//
				if(docs.length>0){
					Guest
					.update({
						lurutime:data.lurutime
					},{$set:data},function(err,docs){
						if(err){
							console.log(err);
							res.send({
								data:err
							});
							return;
						};//if(err)
						res.send({
							code:[1000,'更新成功'],
							data:'更新成功'
						});
					});//Guest
				};//if(docs.length)
				
				});//Guest



			// })
		};//if(req.session)
	},//UpdateTiNeng
}